#include "binFile.h"

void loadBits(std::string file,std::vector<uint32_t> *data )
{
    std::ifstream decompress(file.c_str(),std::ios::in | std::ios::binary);
    uint32_t a;
    if (decompress.is_open())
    {
        while (!decompress.eof())
        {
            decompress.read ((char*)&a, sizeof (uint32_t));
            data->push_back(a);
        }
        decompress.close();
    }
}

void saveBits(std::vector<std::string> output, std::string file)
{

    std::cout<<"start save bits"<<std::endl;

    std::ofstream compress;

    compress.open(file.c_str(), std::ios::out | std::ios::binary);

    uint32_t a=0;
    int count=0;
    if (compress.is_open())
    {
        for(unsigned int j=0; j<output.size(); ++j)
        {
            for(unsigned int i=0; i<output[j].size(); ++i)
            {
                if(output[j][i]=='0')
                {
                    a = a &~(1<<0);
                }else
                {
                    a = a | (1<<0);
                }
                a=a<<1;
                ++count;

                if(count==31)
                {
                    count=0;
                    compress.write((char*)&a,sizeof(uint32_t));
                    a=0;
                }
            }
        }
        a=a<<(31-count);
        count=0;
        compress.write((char*)&a,sizeof(uint32_t));
        a=0;
        compress.close();
    }
    std::cout<<"DONE"<<std::endl;
}

void saveBits(std::vector<bool> output, std::string file)
{

    std::cout<<"start save bits"<<std::endl;

    std::ofstream compress;

    compress.open(file.c_str(), std::ios::out | std::ios::binary);

    uint32_t a=0;
    int count=0;
    if (compress.is_open())
    {
        //for(unsigned int j=0; j<output.size(); ++j)
        //{
            for(unsigned int i=0; i<output.size(); ++i)
            {
                //    std::cout<<output[j][i];
                if(output[i]==0)
                {
                    a = a &~(1<<0);
                }else
                {
                    a = a | (1<<0);
                }
                a=a<<1;
                ++count;

                if(count==31)
                {
                    count=0;
                    compress.write((char*)&a,sizeof(uint32_t));
                    a=0;
                }
            }
            //  std::cout<<std::endl;
        }
        a=a<<(31-count);
        count=0;
        compress.write((char*)&a,sizeof(uint32_t));
        a=0;
        compress.close();

    std::cout<<"DONE"<<std::endl;
}



